package com.harmonyos.controller;

import cn.hutool.json.JSONUtil;

import com.harmonyos.domain.*;
import com.harmonyos.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;


/**
 * @description:
 * @author: Andy
 * @time: 2022/1/2 14:53
 */
@RestController
@CrossOrigin(origins = "*")
@Component
@RequestMapping("api")
@Slf4j
public class Api {
    @Resource
    private TbUserService tbUserService;
    @Resource
    private TbFourmService tbFourmService;
    @Resource
    private TbPsychologicalService tbPsychologicalService;

    @Resource
    private TbTzService service;

    @Resource
    private TbNewsService tbNewsService;
    @Resource
    private TbXinliService tbXinliService;


    /**
     * 添加add
     *
     * @param
     * @return
     */
    @PostMapping("addUser")
    public String addUser(@RequestBody String json) {
        TbUser user = JSONUtil.toBean(json, TbUser.class);
        return tbUserService.insert(user);

    }


    /**
     * 修改
     *
     * @param
     * @return
     */
    @PostMapping("updateUser")
    public String updateUser(@RequestBody String json) {
        TbUser user = JSONUtil.toBean(json, TbUser.class);
        return tbUserService.update(user);

    }

    /**
     * 查询
     *
     * @param
     * @return
     */
    @PostMapping("selectUser")
    public String selectUser(@RequestBody String user) {
        return tbUserService.select(user);
    }

    @PostMapping("selectUserLimit")
    public String selectUserLimit(@RequestBody String json) {
        return tbUserService.selectUserLimit(json);
    }

    @PostMapping("selectByUser")
    public String select(@RequestBody String json) {
        return tbUserService.selectByUser(json);
    }

    @PostMapping("login")
    public String login(@RequestBody String json) {
        return tbUserService.login(json);
    }

    @GetMapping("deleteUser")
    public String delete(String user) {
        return tbUserService.deleteUser(user);
    }


    /**
     * 添加add
     *
     * @param
     * @return
     */
    @PostMapping("addFourm")
    public String addFourm(@RequestBody String json) {
        TbFourm fourm = JSONUtil.toBean(json, TbFourm.class);
        return tbFourmService.insert(fourm);

    }


    /**
     * 删除
     *
     * @param
     * @return
     */
    @PostMapping("delectFourm")
    public String delectFourm(@RequestBody String id) {

        return tbFourmService.delect(id);

    }

    /**
     * 修改
     *
     * @param
     * @return
     */
    @PostMapping("updateFourm")
    public String updateFourm(@RequestBody String json) {
        TbFourm tbFourm = JSONUtil.toBean(json, TbFourm.class);
        return tbFourmService.update(tbFourm);

    }

    /**
     * 查询
     *
     * @param
     * @return
     */
    @PostMapping("selectFourm")
    public String selectFourm(@RequestBody String user) {
        return tbFourmService.select(user);

    }

    /**
     * 查询
     *
     * @param
     * @return
     */
    @GetMapping("selectFourmlist")
    public String selectFourmlist(@RequestParam("tzid") String tizid) {
        return tbFourmService.selecttizi(tizid);

    }

    /**
     * 查询所有
     *
     * @param
     * @return
     */
    @GetMapping("selectFourmAll")
    public String selectUser() {

        return tbFourmService.selectAll();

    }

    /**
     * 查询
     *
     * @param
     * @return
     */
    @PostMapping("selectPsychological")
    public String selectPsychological(@RequestBody String user) {

        return tbPsychologicalService.select(user);

    }

    /**
     * 查询所有
     *
     * @param
     * @return
     */
    @GetMapping("selectPsychologicalAll")
    public String selectPsychologicalAll() {
        return tbPsychologicalService.selectAll();
    }

    @PostMapping("selectPsychologicalLimit")
    public String selectPsychologicalLimit(@RequestBody String json) {
        Limit<TbPsychological> limit = JSONUtil.toBean(json, Limit.class);
        return tbPsychologicalService.selectLimit(limit);
    }

    /**
     * 修改
     *
     * @param
     * @return
     */
    @PostMapping("updatePsychological")
    public String updatePsychologica(@RequestBody String json) {
        TbPsychological tbPsychological = JSONUtil.toBean(json, TbPsychological.class);
        return tbPsychologicalService.update(tbPsychological);

    }

    @PostMapping("addPsychological")
    public String addPsychological(@RequestBody String json) {
        TbPsychological tbPsychological = JSONUtil.toBean(json, TbPsychological.class);
        return tbPsychologicalService.addPsychological(tbPsychological);
    }

    @GetMapping("delPsychological")
    public String delPsychological(@RequestParam("id") int id) {
        return tbPsychologicalService.delPsychological(id);
    }

    /**
     * 查询帖子
     *
     * @param
     * @return
     */
    @GetMapping("selecttz")
    public List<TbTz> selecttzAll(@RequestParam("user") String user) {
        if (user.isEmpty()) {
            return service.selectAll();
        } else {
            return service.select(user);
        }
    }

    /**
     * 添加addTz
     *
     * @param
     * @return
     */
    @PostMapping("addTz")
    public String addTz(@RequestBody String json) {
        TbTz tz = JSONUtil.toBean(json, TbTz.class);
        return service.insert(tz);
    }

    /**
     * 修改
     *
     * @param
     * @return
     */
    @PostMapping("updatetz")
    public String updatetz(@RequestBody TbTz json) {
        return service.update(json);
    }

    /**
     * 删除帖子
     */
    @PostMapping("deletetz")
    public String deletetz(@RequestBody TbTz tbTz) {
        return service.delete(tbTz);
    }

    @PostMapping("selectlimit")
    public String selectlimit(@RequestBody String json) {
        return tbNewsService.selectlimit(json);
    }

    @PostMapping("installNews")
    public String installNews(@RequestBody String news) {
        TbNews tbNews = JSONUtil.toBean(news, TbNews.class);
        return tbNewsService.installNews(tbNews);
    }

    @GetMapping("deleteNews")
    public String deleteNews(@RequestParam("id") int id) {
        return tbNewsService.deleteNews(id);
    }

    @PostMapping("updateNews")
    public String updateNews(@RequestBody String json) {
        TbNews tbNews = JSONUtil.toBean(json, TbNews.class);
        return tbNewsService.updateNews(tbNews);
    }

    @GetMapping("selectAllNews")
    public String selectAllNews() {
        String s = tbNewsService.selectAllNews();
        log.info(s);
        return s;
    }

    @GetMapping("deleteById")
    public String deleteById(@RequestParam("id") String id) {
        return tbXinliService.deleteById(id);
    }

    @PostMapping("addXinli")
    public String addXinli(@RequestBody String json) {
        TbXinli tbXinli = JSONUtil.toBean(json, TbXinli.class);
        return tbXinliService.addXinli(tbXinli);
    }

    @PostMapping("updateXinli")
    public String updateXinli(@RequestBody String json) {
        TbXinli tbXinli = JSONUtil.toBean(json, TbXinli.class);
        return tbXinliService.updateXinli(tbXinli);
    }

    @PostMapping("LimitXinli")
    public String limitXinli(@RequestBody String json) {
        return tbXinliService.LimitXinli(json);
    }

    @GetMapping("selectAllXinLi")
    public String selectAllXinLi() {
        return tbXinliService.selectAll();
    }
}
